*Run unweighted choice model analysis
use "conjoint.dta", clear
log using conjoint-replicate.txt, t replace

*Run unweighted analysis for full sample
*Produce Table 2, Model "Full Sample"
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury, vce(robust)
estimates store SelectOLS1

*mfx at sample means
margins project_type project_size property_type infrastructure ///
age_restriction architecture low_income luxury, atmeans

*the probability of selection for no age restriction is about the same as the probability of selecting
*single-family houses
Single fam houses - 54.12%
200 residences - 47.63%
Tear down - 48.55%
Developer infrastructure - 52.01%
No age restriction - 54.27%
No low income - 47.50%


*Run unweighted analysis for homeowners only
*Produce Table 2, Model "Homeowners"
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury if q8 == 1, vce(robust)
estimates store SelectHOLS1

*Run unweighted analysis for non-homeowners only
*Produce Table 2, Model "Non-Homeowners"
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury if q8 == 2, vce(robust)
estimates store SelectNHOLS1

*Unweighted results table
esttab SelectOLS1 SelectHOLS1 SelectNHOLS1 ///
using"ConjointSelectJan21", se title("Conjoint Selection vs. Profile Characteristics - Unweighted") ///
addnotes("All estimates use robust standard errors" ///
"Dependent variable - respondent selected profile 0-no 1-yes") ///
star (* 0.10 ** 0.05 *** 0.01) rtf mtitles("Full Sample" "Homeowners" "Non-Homeowners") ///
stats(N, label("Observations")) label onecell replace nonum

******************************************************
*Calculate weights for selection analysis
gen inc100nh = dp03_0059penh + dp03_0060penh + dp03_0061penh
label var inc100nh "Percent hholds income 100k and over"
gen q7100 = 0
replace q7100 = 1 if q7 > 5
label var q7100 "Income 100k and over, 0-no 1-yes"

gen age45nh = ((((dp05_0012penh + dp05_0013penh + dp05_0014penh + dp05_0015penh ///
+ dp05_0016penh + dp05_0017penh)/100)*dp05_0001enh)/dp05_0087enh) * 100
label var age45nh "Citizen 18+ pop 45 and over, percent"
gen q945 = 0
replace q945 = 1 if q9 > 2
label var q945 "Age 45 and over, 0-no 1-yes"

gen q11hs = 0
replace q11hs = 1 if q11 > 1
label var q11hs "HS grad or higher, 0-no 1-yes"

ipfweight q3 q4 q7100 q8 q945 q10 q11hs, gen(wgt) val(93.7 6.3 30.2 28.2 35.3 6.3 ///
63.8 36.2 28.7 71.3 39.4 60.6 49.2 50.8 6.9 93.1) maxit(25)

*Run weighted analysis for full sample
*Produce Appendix Table 3
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt], vce(robust)
estimates store SelectWtOLS1

*Run weighted analysis for homeowners
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt] if q8 == 1, vce(robust)
estimates store SelectWtHOLS1

*Run weighted analysis for non-homeowners
set more off
reg selected i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt] if q8 == 2, vce(robust)
estimates store SelectWtNHOLS1

*Weighted results table
esttab SelectWtOLS1 SelectWtHOLS1 SelectWtNHOLS1 ///
using"ConjointSelectJan21Weighted", se title("Conjoint Selection vs. Profile Characteristics - Weighted") ///
addnotes("All estimates use robust standard errors" ///
"Dependent variable - respondent selected profile 0-no 1-yes") ///
star (* 0.10 ** 0.05 *** 0.01) rtf mtitles("Full Sample" "Homeowners" "Non-Homeowners") ///
stats(N, label("Observations")) label onecell replace nonum

*******************************************************
*Ratings analysis - need to do some data cleaning before running analysis
use "conjoint.dta", clear

*Get rid of observations where a respondent rated all 8 profiles 5
count if rating == 5
*512 observations with rating of 5, this is about 1/8th of the sample
*After running command below, drop any respondents who had a frequency of 8
set more off
bysort rating: tab respondentindex if rating == 5
*respondentindex: 70, 88, 223, 291, 308, 359, 360, 466, 503
*Drop the 9 respondents above, check again
drop if respondentindex == 70 | respondentindex == 88 | respondentindex == 223 | respondentindex == 291 ///
| respondentindex == 308 | respondentindex == 359 | respondentindex == 360 | respondentindex == 466 ///
| respondentindex == 503
*72 observations deleted - none left with 8 frequency

*Drop if rated is lower for selected profile by task - only drop that one observation
sort respondentindex task selected
*Create new rating variable - diff between selected and non-selected task 
gen ratingdiff = .
order fips q2_1 respondentindex task profile selected rating ratingdiff
replace ratingdiff = rating - rating[_n-1] if selected == 1
count if ratingdiff < 0
*163 instances! Have to also drop obs for non-selected for task
replace ratingdiff = 999 if ratingdiff[_n+1] < 0
drop if ratingdiff < 0 | ratingdiff == 999
*326 observations deleted
drop ratingdiff

*Unweighted rating analysis for full sample
*Produce Appendix Table 2
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury, vce(robust)
estimates store RatingOLS1

*Unweighted rating analysis for homeowners
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury if q8 == 1, vce(robust)
estimates store RatingHOLS1

*Unweighted rating analysis for non-homeowners
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury if q8 == 2, vce(robust)
estimates store RatingNHOLS1

*Unweighted rating results table
esttab RatingOLS1 RatingHOLS1 RatingNHOLS1 ///
using"ConjointRatingJan21", se title("Conjoint Rating vs. Profile Characteristics - Unweighted") ///
addnotes("All estimates use robust standard errors" ///
"Dependent variable - respondent rating of profile 0-10" ///
"Observations dropped if respondent rated all 8 profiles 5" ///
"Observations dropped if respondent rated selected profile lower than non-selected") ///
star (* 0.10 ** 0.05 *** 0.01) rtf mtitles ("Full Sample" "Homeowners" "Non-Homeowners") ///
stats(N, label("Observations")) label onecell replace nonum


************************************************
*Calculate weights for rating analysis

gen inc100nh = dp03_0059penh + dp03_0060penh + dp03_0061penh
label var inc100nh "Percent hholds income 100k and over"
gen q7100 = 0
replace q7100 = 1 if q7 > 5
label var q7100 "Income 100k and over, 0-no 1-yes"

gen age45nh = ((((dp05_0012penh + dp05_0013penh + dp05_0014penh + dp05_0015penh ///
+ dp05_0016penh + dp05_0017penh)/100)*dp05_0001enh)/dp05_0087enh) * 100
label var age45nh "Citizen 18+ pop 45 and over, percent"
gen q945 = 0
replace q945 = 1 if q9 > 2
label var q945 "Age 45 and over, 0-no 1-yes"

gen q11hs = 0
replace q11hs = 1 if q11 > 1
label var q11hs "HS grad or higher, 0-no 1-yes"

ipfweight q3 q4 q7100 q8 q945 q10 q11hs, gen(wgt) val(93.7 6.3 30.2 28.2 35.3 6.3 ///
63.8 36.2 71.3 28.7 39.4 60.6 49.2 50.8 6.9 93.1) maxit(25)

*Weighted rating analysis for full sample
*Produce Appendix Table 4
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt], vce(robust)
estimates store RatingWtOLS1

*Weighted rating analysis for homeowners
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt] if q8 == 1, vce(robust)
estimates store RatingWtHOLS1

*Weighted rating analysis for non-homeowners
set more off
reg rating i.project_type i.project_size i.property_type i.infrastructure ///
i.age_restriction i.architecture i.low_income i.luxury [pw=wgt] if q8 == 2, vce(robust)
estimates store RatingWtNHOLS1

*Weighted rating results table
esttab RatingWtOLS1 RatingWtHOLS1 RatingWtNHOLS1 ///
using"ConjointRatingJan21Weighted", se title("Conjoint Rating vs. Profile Characteristics - Weighted") ///
addnotes("All estimates use robust standard errors" ///
"Dependent variable - respondent rating of profile 0-10" ///
"Observations dropped if respondent rated all 8 profiles 5" ///
"Observations dropped if respondent rated selected profile lower than non-selected") ///
star (* 0.10 ** 0.05 *** 0.01) rtf mtitles ("Full Sample" "Homeowners" "Non-Homeowners") ///
stats(N, label("Observations")) label onecell replace nonum


log close